import java.awt.Point;
import java.io.Serializable;
import java.net.InetAddress;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;

public class ServerMessageSender {
	TreeMap tm = new TreeMap();
	NetworkSender ns;
	SQLDatabase sd;
	public ServerMessageSender(NetworkSender ns1, SQLDatabase sd1){
		ns = ns1;
		sd = sd1;
	}
	private void addClient(InetAddress ip, int id){
		tm.put(new Integer(id),ip);
	}
	private void removeClient(int id){
		tm.remove(new Integer(id));
	}
	public void playerHealthUpdate(InetAddress ip, int id, int health){
		try{
		ServerPlayerHealthUpdate p = new ServerPlayerHealthUpdate(id, health);	
		sd.setHP(id,health);
		ns.send(p,ip);
		}
		catch(Exception e){
			System.out.println(e);
		}
	}
	public void playerPositionUpdate(int id, int x, int y){
		ServerPlayerPositionUpdate p = new ServerPlayerPositionUpdate(id, x,y);
		sd.setLoc(id,x,y);
		sendMessageToAll(p);
	}
	public void specificPlayerPositionUpdate(int id, int x, int y, InetAddress targetIp){
		ServerPlayerPositionUpdate p = new ServerPlayerPositionUpdate(id, x ,y);
		ns.send(p,targetIp);
	}
	private void playerExistenceUpdate(int id, boolean alive){
		try{
		ServerPlayerExistenceUpdate p = new ServerPlayerExistenceUpdate(id, alive);
		if(alive == true)
			sd.setAlive(id,1);
			else
			sd.setAlive(id,0);
		sendMessageToAll(p);	
		}
		catch(Exception e){
			System.out.println(e);
		}
	}
	public void playerAbilityUpdate(int id, int type, int x, int y){
		ServerPlayerAbilityMessage p = new ServerPlayerAbilityMessage(id,type, x, y);
		sendMessageToAll(p);
	}
	public void playerManaUpdate(InetAddress ip, int id, int mana){
		ServerPlayerManaUpdate p = new ServerPlayerManaUpdate(id, mana);
		sd.setMP(id,mana);
		ns.send(p,ip);
	}
	public void playerInventoryUpdate(InetAddress ip,int id,int hat,int weapon,int accessory, int manaPot, int healthPot){
		try{
			ServerPlayerInventoryUpdate p = new ServerPlayerInventoryUpdate(id, hat, weapon, accessory, manaPot,healthPot);
		sd.setHat(id,hat);
		sd.setWeapon(id,weapon);
		sd.setHPPot(id,manaPot);
		sd.setMPPot(id,healthPot);
		ns.send(p,ip);
	}
	catch(Exception e){
		System.out.println(e);
	}
	}
	public void GoodLogin(InetAddress ip, PlayerEntityData d){
		ServerPlayerSuccessfulLoginUpdate p = new ServerPlayerSuccessfulLoginUpdate(d.id,d);
		ns.send(p,ip);
	}
	public void monsterHealthUpdate(int id, int health){
		try{
		ServerMonsterHealthUpdate p = new ServerMonsterHealthUpdate(id, health);
		sd.setHP(id,health);
		sendMessageToAll(p);
		}
		catch(Exception e){
			System.out.println(e);
		}
	}
	public void monsterPositionUpdate(int id, int x, int y){
		try{
		ServerMonsterMovementUpdate p = new ServerMonsterMovementUpdate(id,x,y);
		sd.setLoc(id,x,y);
		sendMessageToAll(p);
		}
		catch(Exception e){
			System.out.println(e);
		}
	}
	private void monsterExistenceUpdate(int id, boolean alive){
		try{
		ServerMonsterExistenceUpdate p = new ServerMonsterExistenceUpdate(id, alive);
		if(alive == true)
		sd.setAlive(id,1);
		else
		sd.setAlive(id,0);
		sendMessageToAll(p);
		}
		catch(Exception e){
			System.out.println(e);
		}
	}
	public void monsterAbilityUpdate(int id, Point ability, EntityData enemy){
		ServerMonsterAbilityUpdate p = new ServerMonsterAbilityUpdate(id, ability, enemy);
		sendMessageToAll(p);
	}
	public void playerEntityData(InetAddress ip, EntityData e){
		ServerPlayerDataMessage p = new ServerPlayerDataMessage(e);
		ns.send(p,ip);
	}
	private void sendMessageToAll(Serializable m){
		Set s = tm.keySet();
		Iterator i = s.iterator();
		System.out.println("sending message to all: " + m.getClass());
		while(i.hasNext()){
			Integer temp = (Integer)i.next();
			InetAddress tempip= (InetAddress)tm.get(temp);
			ns.send(m, tempip);
		}
	}
	
	public void addPlayer(InetAddress ip, PlayerEntityData pdata){
		try{
		addClient(ip, pdata.id);
		playerExistenceUpdate(pdata.id, true);
		playerPositionUpdate(pdata.id, pdata.location.x, pdata.location.y);
		sd.addName(pdata.name,pdata.type,"");
		GoodLogin(ip, pdata);
		}
		catch(Exception e){
			System.out.println(e);
		}
	}
	public void removePlayer(PlayerEntityData pdata){
		try{
		removeClient(pdata.id);
		playerExistenceUpdate(pdata.id, false);
		}
		catch(Exception e){
			System.out.println(e);
		}
	}
	
	public void addMonster(MonsterEntityData data){
		try{
		monsterExistenceUpdate(data.id, true);
		monsterPositionUpdate(data.id, data.location.x, data.location.y);
		}
		catch(Exception e){
			System.out.println(e);
		}
	}
	public void removeMonster(MonsterEntityData data){
		try{
		monsterExistenceUpdate(data.id, false);
		}
		catch(Exception e){
			System.out.println(e);
		}
	}
	public void specificPlayerPlayerUpdate(PlayerEntityData pdata){
		ServerPlayerInitialUpdate m = new ServerPlayerInitialUpdate(pdata.id,pdata.location.x,pdata.location.y,pdata.type,true);
		ns.send(m,(InetAddress)tm.get(pdata.id));
	}

	public void specificPlayerMonsterUpdate(MonsterEntityData mdata, InetAddress ip){
		ServerPlayerInitialUpdate m = new ServerPlayerInitialUpdate(mdata.id,mdata.location.x,mdata.location.y,mdata.type,false);
		ns.send(m,ip);
	}
}
